package com.libraries.view;


import com.libraries.helper.Logger;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.widget.HorizontalScrollView;

@SuppressLint("NewApi")
public class CustomHScrollView extends HorizontalScrollView {
	// 滑动距离及坐标  
    private float xDistance, yDistance, xLast, yLast;  
	public CustomHScrollView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	@Override  
    public boolean onInterceptTouchEvent(MotionEvent ev) {  
        switch (ev.getAction()) {  
            case MotionEvent.ACTION_DOWN:  
                xDistance = yDistance = 0f;  
                xLast = ev.getX();  
                yLast = ev.getY();  
                break;  
            case MotionEvent.ACTION_MOVE:  
                final float curX = ev.getX();  
                final float curY = ev.getY();  
                  
                xDistance += Math.abs(curX - xLast);  
                yDistance += Math.abs(curY - yLast);  
                xLast = curX;  
                yLast = curY;  
                  
                if(yDistance> xDistance ){  
                    return false;  
                }    
        }  
  
        return super.onInterceptTouchEvent(ev);  
    }  

	class XScrollDetector extends SimpleOnGestureListener {

		@Override
		public boolean onScroll(MotionEvent e1, MotionEvent e2,
				float distanceX, float distanceY) {
			Logger.log(this, "onScroll", distanceX+"/"+distanceY);
			/**
			 * 如果我们滚动更接近水平方向,返回false,让子视图来处理它
			 */
			return (Math.abs(distanceY) > Math.abs(distanceX));
		}

		@Override
		public boolean onDown(MotionEvent e) {
			Logger.log(this, "onDown", e.getAction());
			return super.onDown(e);
		}

		@Override
		public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
				float velocityY) {
			Logger.log(this, "onFling", e1.getAction());
			return super.onFling(e1, e2, velocityX, velocityY);
		}

		@Override
		public boolean onSingleTapUp(MotionEvent e) {
			Logger.log(this, "onFling", e.getAction());
			return super.onSingleTapUp(e);
		}
		
	}
}
